package com.guanzi.server.controller;

import com.guanzi.server.entity.User;
import com.guanzi.server.service.IUserService;
import com.guanzi.server.util.RespBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author Xg
 * @date 2021/6/4 13:35
 */
@RestController
public class LoginController {

    @Autowired
    private IUserService userService;

    /**
     * 没有的话就注册，有就登录
     * @param username
     * @return
     */
    @PostMapping("/login")
    public RespBean login( @RequestParam("username") String username ) {
        //接收用户名，验证是否存在
        User user = userService.login(username);
        if ( null == user ) {
            User userRegister = userService.register(username);
            if ( null == userRegister ) {
                return RespBean.error("登录失败！");
            }
            return RespBean.success("登录成功",userRegister);
        }
        return RespBean.success("登录成功！", user);
    }
}
